using System.Collections.Generic;
using CardGameObject.Common;
using Entity;
using Mapper;
using UnityEngine;

namespace CardGameObject.DeckScene
{
    public class DeckManager : MonoBehaviour
    {
        public GameObject CollectDeckPrefab;

        private readonly List<GameObject> collectPositionList = new List<GameObject>();
        private readonly List<GameObject> collectDeckList = new List<GameObject>();
    
        private const int COLLECT_DECK_NUMBER = 10;

        private static DeckManager instance;

        private static int current = 1;
        private static int pageSize = 10;

        private void Awake()
        {
            if (instance == null)
            {
                instance = this;
            }
            for (int i = 1; i <= COLLECT_DECK_NUMBER; i++)
            {
                GameObject collectPosition = GameObject.Find("CollectPosition_" + i);
                collectPositionList.Add(collectPosition);
            }
            ShowCollectDecks(current , pageSize);
        }

        public static DeckManager GetInstance()
        {
            if (instance == null)
            {
                Debug.LogError("��ǰ��������DeckSelect�������벻Ҫ��ȡDeckSelect�����µĵ���");
            }
            return instance;
        }

        public GameObject CreateObjectOnPosition(GameObject gameObject , GameObject position)
        {
            Transform positionTransform = position.transform;
            Vector3 positionPosition = positionTransform.position;
            GameObject newDeck = Instantiate(CollectDeckPrefab, positionPosition, positionTransform.rotation);
            return newDeck;
        }

        /// <summary>
        /// չʾ��ǰ�û��Ŀ�����Ϣ
        /// </summary>
        /// <param name="current"></param>
        /// <param name="pageSize"></param>
        public void ShowCollectDecks(int current , int pageSize)
        {
            // ���֮ǰչʾ��Deck��Ϣ
            ClearCollectDecks();
            // ��ȡ��ҳ����
            current = current <= 0 ? 1 : current;
            // ��ѯDeck��Ϣ
            DeckMapper deckMapper = new DeckMapper();
            List<Deck> collectDecks = deckMapper.GetDecksPageByUserId(1 , current, pageSize);
            // ����Deck��������չʾ
            for(int i = 0; i<collectDecks.Count; i++)
            {
                GameObject deck = CreateObjectOnPosition(CollectDeckPrefab, collectPositionList[i]);
                CollectDeck collectDeck = deck.GetComponent<CollectDeck>();
                collectDeck.Deck = collectDecks[i];
                collectDeck.SetDeckInfo();
                collectDeckList.Add(deck);
            }
        }

        public void OnShowNextPageDecks()
        {
            current++;
            ShowCollectDecks(current , pageSize);        
        }

        public void OnShowPrePageDecks()
        {
            current = current <= 0 ? 1 : current-1;
            ShowCollectDecks(current, pageSize);
        }

        public void OnExit()
        {
            GlobalSceneManager.LoadSceneByName("MainScene");
        }

        public void ClearCollectDecks()
        {
            for (int i = collectDeckList.Count-1; i >= 0; i--)
            {
                Destroy(collectDeckList[i]);
                collectDeckList.RemoveAt(i);
            }
        }
    }
}
